/*
 *  newgame_window.cpp
 *  by Lucio
 */

#include <iostream>
#include <unistd.h>
#include <vector>
#include "client_app.h"
#include "client_window.h"
#include "campaign_info.h"
#include "newgame_window.h"
#include "../../common/src/debug.h"
#include "../../common/src/lock.h"
#include "../../common/src/synchro_vector.h"

NewGameWindow::NewGameWindow(BaseObjectType* object,
                        	 const Glib::RefPtr<Gtk::Builder>& builder)
                            :ClientWindow(object, builder) {
    tv_loaded = false;
	builder->get_widget("NameEntry", eGameName);
	builder->get_widget("CreateGameButton", btnCreateGame);
    builder->get_widget("CancelButton", btnCancel);
	createCampaingTreeView(builder);
//	builder->get_widget("CreatePreviewImage",this->PreviewImage);

	btnCreateGame->signal_clicked().connect(sigc::mem_fun(*this,
                                                 &NewGameWindow::onCreateGame));
	btnCancel->signal_clicked().connect(sigc::mem_fun(*this,
                                                     &NewGameWindow::onCancel));
	builder->get_widget_derived("StartGameWindow", winStartGame);
/*
	this->CampaingList->signal_cursor_changed().connect
			(sigc::mem_fun(*this, &CreateGameWindow::ChangePreviewImage));

	this->signal_hide().connect
			(sigc::mem_fun(*this, &CreateGameWindow::WindowSignalHide));
*/
    eGameName->set_text("Borneo");
}

NewGameWindow::~NewGameWindow() {}

void NewGameWindow::setApp(ClientApp* app) {
    this->app = app;
    winStartGame->setApp(app);
    app->addListener(this);
    app->addListener(winStartGame);
}

void NewGameWindow::setLobbyWindow(Gtk::Window* window) {
    winLobby = window;
    winStartGame->setLobbyWindow(window);
}

void NewGameWindow::createCampaingTreeView(const Glib::RefPtr<Gtk::Builder>& b){
    //build tree view
	list_store = Gtk::ListStore::create(campaign_record);
	b->get_widget("GameCampaingList", tv_campaigns);
	tv_campaigns->set_model(list_store);
	tv_campaigns->append_column("Id", campaign_record.campaignID);
	tv_campaigns->append_column("Name", campaign_record.name);
	tv_campaigns->append_column("Difficult", campaign_record.difficulty);
	tv_campaigns->append_column("LevelSize", campaign_record.level_size);
	tv_campaigns->append_column("MinPlayers", campaign_record.min_players);
}

void NewGameWindow::loadData() {
    Lock lock(&mutex);
    if (tv_loaded) return;
    std::vector<CampaignInfo*>* v = app->getCampaigns().getVector();
    for (int i = 0; i < v->size(); ++i) {
        Gtk::TreeModel::Row row = *(list_store->append());
        row[campaign_record.campaignID] = (*v)[i]->getCampaignID();
        row[campaign_record.name] = (*v)[i]->getName();
        row[campaign_record.difficulty] = (*v)[i]->getDifficulty();
        row[campaign_record.level_size] = (*v)[i]->getLevelsSize();
        row[campaign_record.min_players] = (*v)[i]->getMinPlayers();
        row[campaign_record.img_path] = (*v)[i]->getImageID();
    }
    tv_loaded = true;
}

void NewGameWindow::onCreateGame() {
    Lock lock(&mutex);
    Gtk::TreeModel::Row r = *tv_campaigns->get_selection()->get_selected();
	if (eGameName->get_text() == "" || r == NULL) return;
	std::string campaignID = r->get_value(campaign_record.campaignID).data();
    std::string min_players = r->get_value(campaign_record.min_players).data();
    app->requestCreateGame(eGameName->get_text(), campaignID);
    status = STATUS_WAITING;
    while (status == STATUS_WAITING) {
        usleep(1);
    }
    if (status == STATUS_OK) {
        winStartGame->setGameMinPlayers(min_players);
        winStartGame->show();
        get_application()->add_window(*winStartGame);
        get_application()->remove_window(*this);
        hide();
    }
}

void NewGameWindow::onCancel() {
    winLobby->show();
    get_application()->add_window(*winLobby);
    get_application()->remove_window(*this);
    hide();
}

void NewGameWindow::notifyGameCreateOk(ClientApp* app) {
    status = STATUS_OK;
}

/*
void CreateGameWindow::WindowSignalHide() {
	this->GameNameEntry->set_text("");
	this->PreviewImage->clear();
	this->CampaingList->get_selection()->unselect_all();
}

void CreateGameWindow::ChangePreviewImage() {
	Gtk::TreeModel::Row Row = *CampaingList->get_selection()->get_selected();
	this->PreviewImage->set(Row->get_value(model.ImagePath).data());
}

*/

//{+}


